﻿package com._public._ui
{
	import com._public._filters.useFilter;
	import com._public._transitions.TweenLite;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author WZH(shch8.com)
	 */
	public class ScWindows extends MovieClip
	{
		private var _title:String;
		private var titleText:TextField;
		private var _width:Number=100;
		private var _height:Number=100;
		private var _content:Sprite;
		private var _maskShpae:Sprite;
		private const TITLE_HEIGHT:uint = 30;
		private const PADDING:uint = 5;
		private var _base:DisplayObjectContainer;
		private var _isOpen:Boolean;
		private var _shimShape:Sprite;//禁背影层
		private var _useShim:Boolean = false;
		private var useAminate:Boolean = false;
		private var face:Sprite
		public function ScWindows(_face:Sprite) 
		{
			face = _face
			addChild(face)
			closeButton.stop();
			closeButton.addEventListener(MouseEvent.CLICK, closeEvent);
			closeButton.addEventListener(MouseEvent.MOUSE_OVER, overFun)
			closeButton.addEventListener(MouseEvent.MOUSE_OUT, outFun)
			//new useFilter(bgShape, "shadow", { distance:3, angle:60, color:0x000000, alpha:0.4, strength:0.2 } );
			dragEnabled = true;
		}
		public function open(base:DisplayObjectContainer):void
		{
			if (_base == null) {
				this.x = (base.width - width) / 2;
				this.y = 100;
				}
			_base = base;
			
			if (_useShim) {
				if (_shimShape == null) {
					_shimShape = new Sprite;
					_base.addChild(_shimShape)
				}
				_shimShape.graphics.clear();
				_shimShape.graphics.beginFill(0xffffff, 0.6);
				_shimShape.graphics.drawRect(0, 0, 2000, 1500);
				_shimShape.visible = true;
			if (useAminate) {
				_shimShape.alpha = 0;
				TweenLite.to(_shimShape, 0.8, { alpha:1 } );
			}
				}
			
			_base.addChild(this);
			
			if (useAminate) {
			this.alpha = 0;
			TweenLite.to(this, 0.3, { alpha:1 } );
			}
			isOpen = true;
		}
		public function close():void
		{
			if (_base == null) return;
			if(useAminate){
			TweenLite.to(this, 0.3, { alpha:0, onComplete:closeCom } );
			TweenLite.to(_shimShape, 0.3, { alpha:0 } );
			}else {
				closeCom();
				}
			isOpen = false;
		}
		private function closeCom():void
		{
			
			_base.removeChild(this);
			if (_shimShape) {
				_base.removeChild(_shimShape);
				_shimShape = null;
			}
			_base = null;
		}
		public function addContent(value:Sprite):void
		{
			if (_content) {
				this.removeChild(_content);
				}
			_content = value;
			_content.x = PADDING;
			_content.y = TITLE_HEIGHT;
			this.addChild(_content);
			draw();
		}
		public function getContent():DisplayObjectContainer
		{
			return _base;
		}
		public function get isOpen():Boolean
		{
			return _isOpen;
		}
		public function set isOpen(value:Boolean):void
		{
			_isOpen = value;
		}
		public function set useShim(value:Boolean):void
		{
			_useShim= value;
		}
		override public function get width():Number { return _width; }
		
		override public function set width(value:Number):void 
		{
			if (value < 150) value = 150;
			_width = value;
		}
		override public function get height():Number { return _height; }
		
		override public function set height(value:Number):void 
		{
			if (value < 150) value = 150;
			_height = value;
		}
		private function get closeButton():MovieClip
		{
			return face.getChildByName("closeBut") as MovieClip;
		}
		private function get bgShape():MovieClip
		{
			return face.getChildByName("bg") as MovieClip;
		}
		public function set dragEnabled(value:Boolean):void
		{
			if (value) {
				bgShape.addEventListener(MouseEvent.MOUSE_DOWN, bgDown);
				}else {
					bgShape.removeEventListener(MouseEvent.MOUSE_DOWN, bgDown);
				}
		}
		
		private function bgDown(event:MouseEvent):void
		{
			if(this.mouseY<TITLE_HEIGHT){
				stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
				this.startDrag()
				bgShape.alpha = 0.7;
			}
		}
		private function mouseUp(event:MouseEvent):void
		{
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			bgShape.alpha = 1;
		}
		
		private function overFun(event:MouseEvent):void {
			closeButton.gotoAndPlay(2)
		}
		private function outFun(event:MouseEvent):void {
			closeButton.gotoAndPlay("tag")
		}
		public function set title(value:String):void
		{
			if (value == "" || value == null) return;
			_title = value;
			if (titleText == null) {
				titleText = new TextField;
				titleText.defaultTextFormat = new TextFormat("宋体", 13, 0xeeeeee, true);
				titleText.autoSize=TextFieldAutoSize.LEFT
				titleText.mouseEnabled = false;
				new useFilter(titleText, "glow", { color:0x999999, alpha:0.2} );
				titleText.x =15
				titleText.y = 8;
				addChild(titleText)
				}
			titleText.htmlText = title;
		}
		public function get title():String
		{
			return _title
		}
		private function closeEvent(event:MouseEvent):void
		{
			dispatchEvent(new Event(Event.CLOSE));
			close();
		}
		private function draw():void
		{
			if(titleText){
			if (titleText.textWidth > width) {
				titleText.width = width;
				titleText.autoSize = TextFieldAutoSize.NONE;
			}
			}

			this.graphics.clear();
			this.graphics.beginFill(0x999999,0.1);
			this.graphics.drawRoundRect(0, 0, width, height,3,3);
			bgShape.width = width;
			bgShape.height = height;
			closeButton.x = width - 30;
		}
		
	}
	
}